package edu.fdzc.learn.controller;

import edu.fdzc.learn.common.Result;
import edu.fdzc.learn.entity.Course;
import edu.fdzc.learn.entity.vo.CouTeaLesVo;
import edu.fdzc.learn.service.CourseService;
import edu.fdzc.learn.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/course")
public class CourseController {

    @Autowired
    private CourseService courseService;

    @PostMapping("/add")
    public Result addCourse(@RequestBody Course course){
        Map<String ,Object> map = ThreadLocalUtil.get();
        int id = (int) map.get("id");
        course.setTeacherId(id);
        System.out.println(id);
        courseService.insert(course);
        return Result.success();
    }

    @GetMapping
    public Result<List<Course>> findAll(){
        List<Course> list = courseService.findall();
        return Result.success(list);
    }

    @GetMapping("/{id}")
    public Result<Course> findById(@PathVariable int id){
        Course course = courseService.findById(id);
        return Result.success(course);
    }

    @PutMapping("/update")
    public Result updateCourse(@RequestBody Course course){
        courseService.update(course);
        return Result.success();
    }

    @DeleteMapping("/delete")
    public Result deleteById(@RequestParam int id){
        courseService.deleteById(id);
        return Result.success();
    }
    @DeleteMapping("/deleteall")
    public Result deleteByIds(int[] ids){
        courseService.deleteByIds(ids);
        return Result.success();
    }

    @GetMapping("/detail/{courseId}")
    public Result<CouTeaLesVo> findAllTeaLes(@PathVariable int courseId){
        CouTeaLesVo ctl = courseService.findAllTeaLes(courseId);
        return Result.success(ctl);
    }

}
